import 'package:flutter/material.dart';

import '../helpers/database_helper.dart';

class ShowData extends StatefulWidget {
  const ShowData({super.key});

  @override
  State<ShowData> createState() => _ShowDataState();
}

class _ShowDataState extends State<ShowData> {
  final dbHelper = DatabaseHelper();
  late int currentBook=1;

  _addInfo() async {
    // 插入书籍和章节
    currentBook = await dbHelper.insertBook({'title': 'Flutter in Action', 'author': 'Eric Windmill'});
    await dbHelper.insertChapter({'book_id': currentBook, 'title': 'Chapter 1', 'content': 'Introduction to Flutter'});
    await dbHelper.insertChapter({'book_id': currentBook, 'title': 'Chapter 2', 'content': 'practice to Flutter'});
    await dbHelper.insertChapter({'book_id': currentBook, 'title': 'Chapter 3', 'content': 'become master to Flutter'});
  }

  _getInfo() async {
    final books = await dbHelper.getBooks();
    print('Books: $books');
    final chapters = await dbHelper.getChaptersByBookId(currentBook);
    print('Chapters: $chapters');
  }

  _updateInfo() {
    // 更新书籍（示例）
    // await dbHelper.updateBook({'id': 1, 'title': 'Updated Title', 'author': 'Same Author'});
  }

  _deleteInfo() {
    // 删除章节（示例）
    // await dbHelper.deleteChapter(1);
  }


  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('测试数据'),
      ),
      body: Center(
        child: Row(
          mainAxisAlignment: MainAxisAlignment.spaceAround,
          children: [
            ElevatedButton(
              onPressed: _addInfo,
              child: Text('Add'),
            ),
            ElevatedButton(
              onPressed: _getInfo,
              child: Text('Get'),
            ),
            ElevatedButton(
              onPressed: _updateInfo,
              child: Text('Update'),
            ),
            ElevatedButton(
              onPressed: _deleteInfo,
              child: Text('Delete'),
            ),
          ],
        ),
      ),
    );
  }
}
